package com.leboncoin.html;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.leboncoin.bean.ClassifiedBean;
import java.util.logging.Level;

public class LeboncoinHtmlSearchResult {

    static final Logger logger = Logger.getLogger(LeboncoinHtmlSearchResult.class.getName());
    List<Element> rootElements;

    public LeboncoinHtmlSearchResult(String htmlSource) {
        super();
        Document document = Jsoup.parse(htmlSource);
        //System.out.println(htmlSource);
        this.rootElements = new ArrayList<>();
        String rootSel = null;
        Elements root = document.select(rootSel = "div[class=list-lbc]");
        if (root != null && root.size() > 0) {
            for (Element div : root) {
                Elements links = div.select("a");
                if (links != null && links.size() > 0) {
                    this.rootElements.add(links.first());
                } 
            }
            if (rootElements.isEmpty()) {
                logger.log(Level.INFO, "This element contains no link! {0}", (root.outerHtml().length() > 100?root.outerHtml().substring(0, 100):root.outerHtml()));
            }
        } else {
            logger.log(Level.INFO, "No root elements! ... selector: {0} html: {1}", new Object[]{rootSel, (document.html().length() > 100?document.html().substring(0, 100):document.html())});
            logger.log(Level.FINE, document.html());
        }
    }

    public List<ClassifiedBean> getClassifiedList(Boolean daySearch) {

        List<ClassifiedBean> classifiedList = new ArrayList<>();

        Elements elements = getFilteredTrList(daySearch); // liste des tr du jour
        for (Element element : elements) {
            ClassifiedBean classifiedBean = new ClassifiedBean();

            if (populateTitle(element, classifiedBean)) {
                classifiedList.add(classifiedBean);
            }
        }
        return classifiedList;
    }

    public Elements getFilteredTrList(Boolean daySearch) {

        Elements result = new Elements();

        for (Element rootElement : rootElements) {

            // Seulement les tr d'aujourd'hui
            Boolean keepThisAd = false;
            if (!daySearch) {
                keepThisAd = true;
            } else {
                Elements dateElements = rootElement.getElementsByAttributeValue("class", "date");
                if (dateElements.size() > 0) {
                    Element dateElement = dateElements.get(0).child(0);
                    if (dateElement.text().contains("Aujourd")) {
                        keepThisAd = true;
                    }
                } else {
                    logger.info("no date elements in html");
                }
            }
            if (keepThisAd) {
                result.add(rootElement);
            }
        }

        return result;
    }

    public Boolean populateTitle(Element element, ClassifiedBean classifiedBean) {

        Boolean isTitle = false; // Ce TD contient-il le titre de l'annonce + prix?

        // Titre 
        Elements aList = element.getElementsByAttributeValue("class", "title");
        for (Element a : aList) {
            if (a.hasText()) {
                // C'est le titre de l'annonce
                classifiedBean.setTitle(a.text());
                isTitle = true;
            }
        }

        // Lien 
        classifiedBean.setLink(element.attr("href"));

        return isTitle;
    }
}
